// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.skinnedmodel.population;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import zombie.core.logger.ExceptionLogger;
import zombie.debug.DebugLog;
import zombie.debug.DebugType;
import zombie.gameStates.ChooseGameInfo;
import zombie.util.PZXmlParserException;
import zombie.util.PZXmlUtil;
import zombie.util.StringUtils;

@XmlRootElement
public class ClothingDecals {
    @XmlElement(
        name = "group"
    )
    public final ArrayList<ClothingDecalGroup> m_Groups = new ArrayList();
    @XmlTransient
    public static ClothingDecals instance;
    private final HashMap<String, ClothingDecals.CachedDecal> m_cachedDecals = new HashMap();

    public static void init() {
        if (instance != null) {
            throw new IllegalStateException("ClothingDecals Already Initialized.");
        } else {
            instance = Parse(
                zombie.ZomboidFileSystem.instance.base.getAbsolutePath()
                    + File.separator
                    + zombie.ZomboidFileSystem.processFilePath("media/clothing/clothingDecals.xml", File.separatorChar)
            );
            if (instance != null) {
                for (String string0 : zombie.ZomboidFileSystem.instance.getModIDs()) {
                    ChooseGameInfo.Mod mod = ChooseGameInfo.getAvailableModDetails(string0);
                    if (mod != null) {
                        String string1 = zombie.ZomboidFileSystem.instance.getModDir(string0);
                        ClothingDecals clothingDecals = Parse(
                            string1 + File.separator + zombie.ZomboidFileSystem.processFilePath("media/clothing/clothingDecals.xml", File.separatorChar)
                        );
                        if (clothingDecals != null) {
                            for (ClothingDecalGroup clothingDecalGroup0 : clothingDecals.m_Groups) {
                                ClothingDecalGroup clothingDecalGroup1 = instance.FindGroup(clothingDecalGroup0.m_Name);
                                if (clothingDecalGroup1 == null) {
                                    instance.m_Groups.add(clothingDecalGroup0);
                                } else {
                                    if (DebugLog.isEnabled(DebugType.Clothing)) {
                                        DebugLog.Clothing.println("mod \"%s\" overrides decal group \"%s\"", string0, clothingDecalGroup0.m_Name);
                                    }

                                    int _int = instance.m_Groups.indexOf(clothingDecalGroup1);
                                    instance.m_Groups.set(_int, clothingDecalGroup0);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static void Reset() {
        if (instance != null) {
            instance.m_cachedDecals.clear();
            instance.m_Groups.clear();
            instance = null;
        }
    }

    public static ClothingDecals Parse(String filename) {
        try {
            return parse(filename);
        } catch (FileNotFoundException fileNotFoundException) {
        } catch (JAXBException | IOException iOException) {
            ExceptionLogger.logException(iOException);
        }

        return null;
    }

    public static ClothingDecals parse(String filename) throws JAXBException, IOException {
        FileInputStream fileInputStream = new FileInputStream(filename);

        ClothingDecals clothingDecals;
        try {
            JAXBContext jAXBContext = JAXBContext.newInstance(ClothingDecals.class);
            Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
            clothingDecals = (ClothingDecals)unmarshaller.unmarshal(fileInputStream);
        } catch (Throwable throwable0) {
            try {
                fileInputStream.close();
            } catch (Throwable throwable1) {
                throwable0.addSuppressed(throwable1);
            }

            throw throwable0;
        }

        fileInputStream.close();
        return clothingDecals;
    }

    public ClothingDecal getDecal(String name) {
        if (StringUtils.isNullOrWhitespace(name)) {
            return null;
        } else {
            ClothingDecals.CachedDecal cachedDecal = (ClothingDecals.CachedDecal)this.m_cachedDecals.get(name);
            if (cachedDecal == null) {
                cachedDecal = new ClothingDecals.CachedDecal();
                this.m_cachedDecals.put(name, cachedDecal);
            }

            if (cachedDecal.m_decal != null) {
                return cachedDecal.m_decal;
            } else {
                String string = zombie.ZomboidFileSystem.instance.getString("media/clothing/clothingDecals/" + name + ".xml");

                try {
                    cachedDecal.m_decal = PZXmlUtil.parse(ClothingDecal.class, string);
                    cachedDecal.m_decal.name = name;
                } catch (PZXmlParserException pZXmlParserException) {
                    System.err.println("Failed to load ClothingDecal: " + string);
                    ExceptionLogger.logException(pZXmlParserException);
                    return null;
                }

                return cachedDecal.m_decal;
            }
        }
    }

    public ClothingDecalGroup FindGroup(String name) {
        if (StringUtils.isNullOrWhitespace(name)) {
            return null;
        } else {
            for (int _int = 0; _int < this.m_Groups.size(); _int++) {
                ClothingDecalGroup clothingDecalGroup = (ClothingDecalGroup)this.m_Groups.get(_int);
                if (clothingDecalGroup.m_Name.equalsIgnoreCase(name)) {
                    return clothingDecalGroup;
                }
            }

            return null;
        }
    }

    public String getRandomDecal(String groupName) {
        ClothingDecalGroup clothingDecalGroup = this.FindGroup(groupName);
        return clothingDecalGroup == null ? null : clothingDecalGroup.getRandomDecal();
    }

    private static final class CachedDecal {
        ClothingDecal m_decal;
    }
}
